package com.example.activity_day1;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	Button btnAdd;
	int count = 0;
	String total = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Bundle b = getIntent().getExtras();
		
		if(b != null){
		 total = b.getString("BACK");
		}
		
		
		//System.out.println("back  " + total);

		if(total != null){
			count = Integer.parseInt(total);
		}
		

		if (count != 0) {
			MyListFragment1 myListFragment = new MyListFragment1();
			Bundle bundle = new Bundle();
			bundle.putInt("KEY_DETAIL", count);
			myListFragment.setArguments(bundle);
			FragmentTransaction fragmentTransaction = getFragmentManager()
					.beginTransaction();

			fragmentTransaction.replace(R.id.fragment1, myListFragment);
			fragmentTransaction.commit();

		}

		btnAdd = (Button) findViewById(R.id.btnAdd);
		btnAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				count++;
				MyListFragment1 myListFragment = new MyListFragment1();
				Bundle bundle = new Bundle();
				bundle.putInt("KEY_DETAIL", count);
				myListFragment.setArguments(bundle);
				FragmentTransaction fragmentTransaction = getFragmentManager()
						.beginTransaction();

				fragmentTransaction.replace(R.id.fragment1, myListFragment);
				fragmentTransaction.commit();
			}
		});

		Button btnGo = (Button) findViewById(R.id.btnGo);

		btnGo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(MainActivity.this, Activity2.class);
				intent.putExtra("TOTAL", count +"");
				startActivity(intent);


				finish();

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
